//The MIT License

//Copyright (c) 2009 Lluis Sanchez, Nick Darnell

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

using System;
using System.IO;

/// <summary>
/// The <see cref="CodeWriter"/> is used to print human readable code formatted to appear
/// similar but not exactly matching C#.  The <see cref="CodeWriter"/> mainly maintains the
/// indention to use when printing blocks of code.
/// </summary>
public class CodeWriter
{
    private TextWriter m_writer;
    private int m_indent;

    /// <summary>
    /// Initializes a new instance of the <see cref="CodeWriter"/> class with a specified
    /// text writer.
    /// </summary>
    /// <param name="tw">The <see cref="TextWriter"/> to initialize the 
    /// <see cref="CodeWriter"/> with.</param>
    public CodeWriter(TextWriter tw)
    {
        m_writer = tw;
    }

    /// <summary>
    /// Begins a new line writing the proper indention at the beginning of the line.
    /// </summary>
    /// <returns>The <see cref="CodeWriter"/>.</returns>
    public CodeWriter BeginLine()
    {
        m_writer.Write(new String(' ', m_indent * 4));
        return this;
    }

    /// <summary>
    /// Writes the provided text.
    /// </summary>
    /// <param name="s">The text to write.</param>
    /// <returns>The <see cref="CodeWriter"/>.</returns>
    public CodeWriter Write(string s)
    {
        m_writer.Write(s);
        return this;
    }

    /// <summary>
    /// Ends the current line.
    /// </summary>
    /// <returns>The <see cref="CodeWriter"/>.</returns>
    public CodeWriter EndLine()
    {
        m_writer.WriteLine();
        return this;
    }

    /// <summary>
    /// Writes a new line.
    /// </summary>
    /// <param name="s">The text to write.</param>
    /// <returns>The <see cref="CodeWriter"/>.</returns>
    public CodeWriter WriteLine(string s)
    {
        BeginLine();
        Write(s);
        EndLine();
        return this;
    }

    /// <summary>
    /// Writes a new line and then adds a level of indention.
    /// </summary>
    /// <param name="s">The text to write.</param>
    /// <returns>The <see cref="CodeWriter"/>.</returns>
    public CodeWriter WriteLineInd(string s)
    {
        WriteLine(s);
        m_indent++;
        return this;
    }

    /// <summary>
    /// Removes a level of indention and then writes a new line of text.
    /// </summary>
    /// <param name="s">The text to write.</param>
    /// <returns>The <see cref="CodeWriter"/>.</returns>
    public CodeWriter WriteLineUnind(string s)
    {
        m_indent--;
        WriteLine(s);
        return this;
    }

    /// <summary>
    /// Adds a new level of indention.
    /// </summary>
    public void Indent()
    {
        m_indent++;
    }

    /// <summary>
    /// Removes a level of indention.
    /// </summary>
    public void Unindent()
    {
        m_indent--;
    }
}